package com.ruoyi.project.modules.virtualUsageFieldMerge.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.po.VirtualUsageParamSourcePo;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.InterfaceObjectParam;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.ObjectParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.virtualUsageFieldMerge.domain.VirtualUsageFieldMerge;
import com.ruoyi.project.modules.virtualUsageFieldMerge.service.IVirtualUsageFieldMergeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 虚拟用况出参融合Controller
 *
 * @author smallrain
 * @date 2022-05-21
 */
@Controller
@RequestMapping("/virtualUsageFieldMerge/virtualUsageFieldMerge")
public class VirtualUsageFieldMergeController extends BaseController {
    private String prefix = "virtualUsageFieldMerge/virtualUsageFieldMerge";

    @Autowired
    private IVirtualUsageFieldMergeService virtualUsageFieldMergeService;

    //@RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:view")
    @GetMapping()
    public String virtualUsageFieldMerge() {
        return prefix + "/virtualUsageFieldMerge";
    }

    /**
     * 查询虚拟用况出参融合列表
     */
    //@RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualUsageFieldMerge virtualUsageFieldMerge) {
        startPage();
        List<VirtualUsageFieldMerge> list = virtualUsageFieldMergeService.selectVirtualUsageFieldMergeList(virtualUsageFieldMerge);
        return getDataTable(list);
    }

    /**
     * 导出虚拟用况出参融合列表
     */
    //@RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:export")
    @Log(title = "虚拟用况出参融合", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualUsageFieldMerge virtualUsageFieldMerge) {
        List<VirtualUsageFieldMerge> list = virtualUsageFieldMergeService.selectVirtualUsageFieldMergeList(virtualUsageFieldMerge);
        ExcelUtil<VirtualUsageFieldMerge> util = new ExcelUtil<VirtualUsageFieldMerge>(VirtualUsageFieldMerge.class);
        return util.exportExcel(list, "虚拟用况出参融合数据");
    }

    /**
     * 新增虚拟用况出参融合
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存虚拟用况出参融合
     */
   // @RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:add")
    @Log(title = "虚拟用况出参融合", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualUsageFieldMerge virtualUsageFieldMerge) {
        return toAjax(virtualUsageFieldMergeService.insertVirtualUsageFieldMerge(virtualUsageFieldMerge));
    }

    /**
     * 修改虚拟用况出参融合
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VirtualUsageFieldMerge virtualUsageFieldMerge = virtualUsageFieldMergeService.selectVirtualUsageFieldMergeById(id);
        mmap.put("virtualUsageFieldMerge", virtualUsageFieldMerge);
        return prefix + "/edit";
    }

    /**
     * 修改保存虚拟用况出参融合
     */
    //@RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:edit")
    @Log(title = "虚拟用况出参融合", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualUsageFieldMerge virtualUsageFieldMerge) {
        return toAjax(virtualUsageFieldMergeService.updateVirtualUsageFieldMerge(virtualUsageFieldMerge));
    }

    /**
     * 删除虚拟用况出参融合
     */
    //@RequiresPermissions("virtualUsageFieldMerge:virtualUsageFieldMerge:remove")
    @Log(title = "虚拟用况出参融合", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(virtualUsageFieldMergeService.deleteVirtualUsageFieldMergeByIds(ids));
    }

    @Resource
    private IInterfaceInfoService interfaceInfoService;
    @Resource
    private IVirtualUsageService virtualUsageService;
    /**
     * 虚拟用况字段融合选择
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldMergeSelect")
    public String fieldMergeSelect(Long fieldId, Long virtualUsageId, ModelMap modelMap) {
        modelMap.put("objectId", fieldId);
        modelMap.put("virtualUsageId", virtualUsageId);
        VirtualUsageFieldMerge virtualUsageFieldMerge = new VirtualUsageFieldMerge();
        virtualUsageFieldMerge.setObjectId(fieldId);
        List<VirtualUsageFieldMerge> virtualUsageFieldMerges = this.virtualUsageFieldMergeService.selectVirtualUsageFieldMergeList(virtualUsageFieldMerge);
        if (CollectionUtil.isNotEmpty(virtualUsageFieldMerges)) {
            modelMap.put("id", virtualUsageFieldMerges.get(0).getId());
            modelMap.put("mergeObjectId", virtualUsageFieldMerges.get(0).getMergeObjectId());
            modelMap.put("mergeVirtualUsageId", virtualUsageFieldMerges.get(0).getMergeVirtualUsageId());
        } else {
            modelMap.put("id", "");
            modelMap.put("mergeObjectId", "");
            modelMap.put("mergeVirtualUsageId", "");
        }
        VirtualUsage virtualUsage = virtualUsageService.selectVirtualUsageById(virtualUsageId);

        VirtualUsage usage = new VirtualUsage();
        usage.setRelationInterfaceId(virtualUsage.getRelationInterfaceId());
        List<VirtualUsage> virtualUsageList = this.virtualUsageService.selectVirtualUsages(usage);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        if (CollectionUtil.isNotEmpty(virtualUsageList)) {
            for (VirtualUsage vu : virtualUsageList) {
                if ("Y".equals(vu.getIsMaster())) {
                    vu.setSlaveInterfaceName(vu.getSlaveInterfaceName()+" （主）");
                }
                if (!vu.getId().equals(virtualUsageId)) {
                    List<InterfaceParam> interfaceParams = this.interfaceInfoService.paramList("3", vu.getId(), "2");
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, vu.getSlaveInterfaceName(), convertObjectParamList(interfaceParams, vu.getId())));
                }
            }
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        return prefix + "/field_merge_choose";
    }


    private List<ObjectParam> convertObjectParamList(List<InterfaceParam> interfaceParams, Long virtualUsageId) {
        List<ObjectParam> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(interfaceParams)) {
            for (InterfaceParam interfaceParam : interfaceParams) {
                if (interfaceParam.getObjectType().equals("2")) {
                    if (CollectionUtil.isNotEmpty(interfaceParam.getChildren())) {
                        for (InterfaceParam child : interfaceParam.getChildren()) {
                            ObjectParam param = new ObjectParam();
                            param.setParamTypeName("数据集字段");
                            param.setParamType("2");
                            param.setId(child.getId());
                            param.setParamName(child.getFieldName() + "(" +interfaceParam.getFieldName()+")");
                            param.setParamEnName(child.getFieldEnName());
                            param.setVirtualUsageId(virtualUsageId);
                            result.add(param);
                        }
                    }
                }
            }
        }
        return result;
    }
}
